package com.moxiao.yfjh.module.blind.service.recommendSetting;

import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.RecommendSettingCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.RecommendSettingExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.RecommendSettingPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.RecommendSettingUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.recommendSetting.RecommendSettingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.recommendSetting.RecommendSettingDO;
import com.moxiao.yfjh.module.blind.dal.mysql.recommendSetting.RecommendSettingMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲超级推荐设置 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class RecommendSettingServiceImpl implements RecommendSettingService {

    @Resource
    private RecommendSettingMapper recommendSettingMapper;

    @Override
    public void createRecommendSetting(List<RecommendSettingCreateReqVO> createReqVO) {
        // 插入
        List<RecommendSettingDO> convert = RecommendSettingConvert.INSTANCE.convert(createReqVO);
        Collection<RecommendSettingDO> recommendSettingDOS = new ArrayList<>(convert);
        recommendSettingMapper.insertBatch(recommendSettingDOS);
    }

    @Override
    public void updateRecommendSetting(List<RecommendSettingUpdateReqVO> updateReqVO) {
       updateReqVO.forEach(recommendSettingUpdateReqVO -> {
           // 校验存在
           validateRecommendSettingExists(recommendSettingUpdateReqVO.getId());
           // 更新
           RecommendSettingDO updateObj = RecommendSettingConvert.INSTANCE.convert(recommendSettingUpdateReqVO);
           recommendSettingMapper.updateById(updateObj);
       });
    }

    @Override
    public void deleteRecommendSetting(Long id) {
        // 校验存在
        validateRecommendSettingExists(id);
        // 删除
        recommendSettingMapper.deleteById(id);
    }

    private void validateRecommendSettingExists(Long id) {
        if (recommendSettingMapper.selectById(id) == null) {
            throw exception(RECOMMEND_SETTING_NOT_EXISTS);
        }
    }

    @Override
    public RecommendSettingDO getRecommendSetting(Long id) {
        return recommendSettingMapper.selectById(id);
    }

    @Override
    public List<RecommendSettingDO> getRecommendSettingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return recommendSettingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RecommendSettingDO> getRecommendSettingPage(RecommendSettingPageReqVO pageReqVO) {
        return recommendSettingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RecommendSettingDO> getRecommendSettingList(RecommendSettingExportReqVO exportReqVO) {
        return recommendSettingMapper.selectList(exportReqVO);
    }

}
